package com.tuanzi.loan.web.bean.receipt.product;

import static com.tuanzi.loan.business.constants.DictionaryCode.RECEIPT_PRODUCT_STATUS;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

import javax.annotation.PostConstruct;
import javax.faces.bean.ViewScoped;

import org.apache.commons.lang3.math.NumberUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

import com.google.common.collect.Lists;
import com.tuanzi.loan.business.entity.receipt.product.ReceiptProductCondition;
import com.tuanzi.loan.business.entity.receipt.product.ReceiptProductInfo;
import com.tuanzi.loan.business.service.receipt.product.ReceiptProductConditionService;
import com.tuanzi.loan.business.service.receipt.product.ReceiptProductInfoService;
import com.tuanzi.loan.core.aspect.Debug;
import com.tuanzi.loan.vo.request.receipt.product.ReceiptProductInfoSearchRequest;
import com.tuanzi.loan.vo.response.receipt.product.ReceiptProductInfoSearchResponse;
import com.tuanzi.loan.web.configuration.JSFListingBean;
import com.tuanzi.loan.web.lazy.receipt.product.ReceiptProductInfoLazyDataModel;
import com.tuanzi.server.loan_base.constant.LoanReceiptProductStatus;
import com.tuanzi.server.loan_base.constant.LoanRedisKey;

import lombok.Data;
import lombok.EqualsAndHashCode;

/**
 * @author Ray 2017/09/21
 */
@Data
@Debug
@Component
@ViewScoped
@EqualsAndHashCode(callSuper = true)
public class ReceiptProductInfoListBean extends JSFListingBean<ReceiptProductInfoSearchRequest, ReceiptProductInfoSearchResponse> {

	private static final long serialVersionUID = 30992312030673477L;

	@Autowired
	private ReceiptProductInfoService receiptProductInfoService;
	@Autowired
	private ReceiptProductConditionService receiptProductConditionService;
	@Autowired
	private StringRedisTemplate stringRedisTemplate;

	@PostConstruct
	private void init() {
		initLazyDataModel();
	}

	public void onReset() {
		initLazyDataModel();
	}

	public void onApprove(String id) {
		addSuccessfullyMessage();
	}

	public void onReject(String id) {
		addSuccessfullyMessage();
	}

	public String onAdd() {
		return redirect("/views/receipt/product/approve/info.xhtml");
	}

	public String onEdit(String id) {
		return redirect("/views/receipt/product/approve/product-manager.xhtml?id=" + id);
	}

	private void initLazyDataModel() {
		$ = new ReceiptProductInfoSearchRequest();
		lazyDataModel = new ReceiptProductInfoLazyDataModel($);
	}

	public void onLine(Long productId) {
		ReceiptProductInfo productInfo = receiptProductInfoService.findOne(productId);
		if (Objects.isNull(productInfo)) {
			addErrorMessage("产品不存在");
		}

		ReceiptProductCondition condition = receiptProductConditionService.findReceiptProductConditionByProductId(productId);
		if (Objects.isNull(condition)) {
			condition = new ReceiptProductCondition();
		}

		receiptProductInfoService.updateProductInfoAndConditionByStatus(productInfo, condition, LoanReceiptProductStatus.ONLINE);
		stringRedisTemplate.delete(LoanRedisKey.LOAN_RECEIPT_PRODUCT_LIST);
		addSuccessfullyMessage();
	}

	public void onlineOneKey(Long productId) {
		receiptProductInfoService.onlineOneKey(productId);
		stringRedisTemplate.delete(LoanRedisKey.LOAN_RECEIPT_PRODUCT_LIST);
		addSuccessfullyMessage();
	}

	public void offlineOneKey(Long productId) {
		receiptProductInfoService.offlineOneKey(productId);
		stringRedisTemplate.delete(LoanRedisKey.LOAN_RECEIPT_PRODUCT_LIST);
		addSuccessfullyMessage();
	}

	private List<Long> getSelectedIds() {
		return selectedRecords.stream().map(ReceiptProductInfoSearchResponse::getId).map(x -> NumberUtils.toLong(x.toString())).collect(Collectors.toList());
	}

	public void offLine(Long productId) {
		ReceiptProductInfo productInfo = receiptProductInfoService.findOne(productId);
		if (Objects.isNull(productInfo)) {
			addErrorMessage("产品不存在");
		}

		ReceiptProductCondition condition = receiptProductConditionService.findReceiptProductConditionByProductId(productId);
		if (Objects.isNull(condition)) {
			condition = new ReceiptProductCondition();
		}

		receiptProductInfoService.updateProductInfoAndConditionByStatus(productInfo, condition, LoanReceiptProductStatus.OFFLINE);
		stringRedisTemplate.delete(LoanRedisKey.LOAN_RECEIPT_PRODUCT_LIST);
		addSuccessfullyMessage();
	}

	public void sortUp(Long productId) {
		receiptProductInfoService.sortUp(productId);
		addSuccessfullyMessage();
	}

	public void sortDown(Long productId) {
		receiptProductInfoService.sortDown(productId);
		addSuccessfullyMessage();
	}

	@Override
	protected List<String> getDictionaryCodes() {
		return Lists.newArrayList(RECEIPT_PRODUCT_STATUS);
	}
}
